package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import qeug.shsh.bpm.Factory.ThirdServiceFactory;
import qeug.shsh.bpm.service.SAPService;

import javax.validation.ValidationException;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.opplugin
 * @className: VoucherBackOp
 * @author: wenyx
 * @description: TODO
 * @date: 2025/2/7 14:13
 * @version: 1.0
 */
public class VoucherBackOp extends AbstractOperationServicePlugIn {

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        DynamicObject dataEntity = e.getDataEntities()[0];
//        //设置FALSE时，为异常，显示红色。TRUE时，为绿色。 //区别：绿色提示语显示时间较短。红色一直存在。
//        this.operationResult.setSuccess(false); //
//        // 异常提示语
//        this.operationResult.setMessage("123"); //
//        // 显示结果
//
        try {
            QFilter[] qFilters = new QFilter[]{new QFilter("id", "=", dataEntity.getLong("id"))};
            DynamicObject gl_voucher = BusinessDataServiceHelper.loadSingle("gl_voucher", qFilters);
            SAPService sapService = ThirdServiceFactory.createSAPService();
            String result = sapService.backVoucher(gl_voucher);
            if (result.equals("E")){
                this.operationResult.setSuccess(true); //
                this.operationResult.setMessage("凭证冲销成功！冲销凭证号已存入该凭证" );
//                throw new ValidationException("凭证冲销成功！冲销凭证号已存入该凭证");
            }else{
                this.operationResult.setSuccess(false); //
                this.operationResult.setMessage("凭证冲销失败！原因为："+result );
//                throw new ValidationException("凭证冲销失败！原因为："+result);
            }
            this.operationResult.setShowMessage(true);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);

    }
}
